package com.example.ams.controller;

import com.example.ams.dto.AttendanceSummaryDTO;
import com.example.ams.service.AttendanceSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考勤统计管理控制器
 * Attendance Summary Controller
 */
@RestController
@RequestMapping("/api/summary")
@CrossOrigin(origins = "*")
public class AttendanceSummaryController {

    @Autowired
    private AttendanceSummaryService summaryService;

    /**
     * 查询统计
     * GET /api/summary?empId=&month=
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getSummary(
            @RequestParam(required = false) Long empId,
            @RequestParam(required = false) String month) {

        try {
            if (month != null && empId == null) {
                // 查询某月所有员工的统计
                List<AttendanceSummaryDTO> summaries = summaryService.getSummariesByMonth(month);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "success");
                response.put("data", summaries);
                response.put("total", summaries.size());
                return ResponseEntity.ok(response);
            } else {
                // 查询单个员工的统计
                AttendanceSummaryDTO summary = summaryService.getSummary(empId, month);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "success");
                response.put("data", summary);
                return ResponseEntity.ok(response);
            }
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 重新计算并保存统计
     * POST /api/summary/rebuild?empId=&month=
     */
    @PostMapping("/rebuild")
    public ResponseEntity<Map<String, Object>> rebuildSummary(
            @RequestParam Long empId,
            @RequestParam String month) {

        try {
            AttendanceSummaryDTO summary = summaryService.rebuildSummary(empId, month);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "统计重新计算成功");
            response.put("data", summary);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }
}

